package lbframework.bean.tableEx;

import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import lbframework.bean.panel.LBPanel;
import lbframework.bean.table.LBColumnTable;
import lbframework.classe.Util;

public class LBTableEx extends LBPanel {

    public LBTableEx() {
        initComponents();

        setRodape();
    }

    public int getLinhaSelecionada() {
        return tblTabela.getLinhaSelecionada();
    }

    public void setLinhaSelecionada(int i_linha) {
        tblTabela.setLinhaSelecionada(i_linha);
    }

    public int getColunaSelecionada() {
        return tblTabela.getColunaSelecionada();
    }

    public void setModel(Object[][] i_dados, Vector<LBColumnTable> i_colunas) throws Exception {
        tblTabela.setModel(i_dados, i_colunas);
        setRodape();
    }

    public void setModel(Vector<Object[]> i_dados, LBColumnTable[] i_colunas) throws Exception {
        tblTabela.setModel(i_dados, i_colunas);
        setRodape();
    }

    public void setModel(Object[][] i_dados, LBColumnTable[] i_colunas) throws Exception {
        tblTabela.setModel(i_dados, i_colunas);
        setRodape();
    }

    public void setModel(Vector<Object[]> i_dados, Vector<LBColumnTable> i_colunas) throws Exception {
        tblTabela.setModel(i_dados, i_colunas);
        setRodape();
    }

    @Override
    public void requestFocus() {
        tblTabela.requestFocus();
    }

    public void clear() throws Exception {
        tblTabela.clear();
        setRodape();
    }

    @Override
    public synchronized void addMouseListener(MouseListener l) {
        tblTabela.addMouseListener(l);
    }

    @Override
    public synchronized void addKeyListener(KeyListener l) {
        tblTabela.addKeyListener(l);
    }

    public TableColumnModel getColumnModel() {
        return tblTabela.getColumnModel();
    }

    public void setRodape() {
        lblTotalItens.setText(String.valueOf(tblTabela.getRowCount()));
        lblLinha.setText(String.valueOf(tblTabela.getSelectedRow() + 1));
    }

    public int getRowCount() {
        return tblTabela.getRowCount();
    }

    public boolean isRowSelected(int i) throws Exception {
        return tblTabela.isRowSelected(i);
    }

    public Object getValueAt(int row, int column) {
        return tblTabela.getValueAt(row, column);
    }

    public void setValueAt(Object aValue, int row, int column) {
        tblTabela.setValueAt(aValue, row, column);
    }

    public void editaCampo(int row, int column) throws Exception {
        boolean toggle = true;
        boolean extend = true;

        tblTabela.changeSelection(row, column, toggle, extend);

        if (tblTabela.editCellAt(row, column)) {
            tblTabela.getEditorComponent().requestFocus();
        }
    }

    //e processFocusEvent() em JTextField:
    @Override
    protected void processFocusEvent(FocusEvent fe) {
        if (fe.getID() == FocusEvent.FOCUS_GAINED) {
            tblTabela.selectAll();
        } else if (fe.getID() == FocusEvent.FOCUS_LOST) { //tblTabela.select(0, 0);
            super.processFocusEvent(fe);
        }
    }

    public void setDefaultRenderer(Class<?> columnClass, TableCellRenderer renderer) {
        tblTabela.setDefaultRenderer(columnClass, renderer);
    }

    public void setRowSelectionInterval(int index0, int index1) {
        tblTabela.setRowSelectionInterval(index0, index1);
    }

    @Override
    public void setEnabled(boolean i_habilita) {
        tblTabela.setEnabled(i_habilita);
    }

    @Override
    public boolean isEnabled() {
        return tblTabela.isEnabled();
    }

    public int convertRowIndexToModel(int viewRowIndex) {
        return tblTabela.convertRowIndexToModel(viewRowIndex);
    }

    public int rowAtPoint(Point point) {
        return tblTabela.rowAtPoint(point);
    }

    public void setAutoResize(boolean i_value){
        if(i_value){
            tblTabela.setAutoResizeMode(WIDTH);
        }
    }

    private void exportar() throws Exception {
        LBTableExportacaoGUI form = new LBTableExportacaoGUI();

        form.setTblTabela(tblTabela);
        form.exibirCampo();

        form.setVisible(true);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblTabela = new lbframework.bean.table.LBTable();
        jPanel3 = new javax.swing.JPanel();
        vRLabel2 = new lbframework.bean.label.LBLabel();
        lblLinha = new lbframework.bean.label.LBLabel();
        jPanel1 = new javax.swing.JPanel();
        vRLabel1 = new lbframework.bean.label.LBLabel();
        lblTotalItens = new lbframework.bean.label.LBLabel();
        vRToolBar1 = new lbframework.bean.toolBar.LBToolBar();
        btnExportar = new lbframework.bean.button.LBButton();

        setBorder(null);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        tblTabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblTabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblTabelaMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblTabelaMousePressed(evt);
            }
        });
        tblTabela.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tblTabelaFocusGained(evt);
            }
        });
        tblTabela.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblTabelaKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tblTabelaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tblTabela);

        jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        vRLabel2.setText("Linha:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblLinha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(vRLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblLinha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        vRLabel1.setText("Total de Itens:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblTotalItens, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblTotalItens, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        btnExportar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/exportar_12.png"))); // NOI18N
        btnExportar.setToolTipText("Exportar p/ arquivo");
        btnExportar.setFocusable(false);
        btnExportar.setPreferredSize(new java.awt.Dimension(18, 18));
        btnExportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportarActionPerformed(evt);
            }
        });
        vRToolBar1.add(btnExportar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(3, 3, 3)
                .addComponent(vRToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(vRToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblTabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblTabelaMouseClicked
}//GEN-LAST:event_tblTabelaMouseClicked

    private void tblTabelaMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblTabelaMousePressed
        setRodape();
}//GEN-LAST:event_tblTabelaMousePressed

    private void tblTabelaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblTabelaKeyPressed
}//GEN-LAST:event_tblTabelaKeyPressed

    private void tblTabelaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblTabelaKeyReleased
        setRodape();
}//GEN-LAST:event_tblTabelaKeyReleased

    private void tblTabelaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tblTabelaFocusGained
        setRodape();
}//GEN-LAST:event_tblTabelaFocusGained

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
    }//GEN-LAST:event_formKeyPressed

    private void btnExportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportarActionPerformed
        try {
            exportar();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_btnExportarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton btnExportar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private lbframework.bean.label.LBLabel lblLinha;
    private lbframework.bean.label.LBLabel lblTotalItens;
    private lbframework.bean.table.LBTable tblTabela;
    private lbframework.bean.label.LBLabel vRLabel1;
    private lbframework.bean.label.LBLabel vRLabel2;
    private lbframework.bean.toolBar.LBToolBar vRToolBar1;
    // End of variables declaration//GEN-END:variables

    public boolean isHabilitaOrdenacao() {
        return tblTabela.isHabilitaOrdenacao();
    }

    public void setHabilitaOrdenacao(boolean habilitaOrdenacao) {
        tblTabela.setHabilitaOrdenacao(habilitaOrdenacao);
    }

    public boolean isShowGridLine() {
        return tblTabela.isShowGridLine();
    }

    public void setShowGridLine(boolean showGridLine) {
        tblTabela.setShowGridLine(showGridLine);
    }

    public boolean isMultiSelect() {
        return tblTabela.isMultiSelect();
    }

    public void setMultiSelect(boolean multiSelect) {
        tblTabela.setMultiSelect(multiSelect);
    }

    public void setRowHeight(int rowHeight) {
        tblTabela.setRowHeight(rowHeight);
    }
}
